import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import gui.EarthPanel;
import gui.UserControlPanel;

import common.Constants;
import common.EarthImage;
import common.SimulationSettings;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class EarthSim extends JFrame {
  private static final long serialVersionUID = 3386230071400230901L;

  private Container contentPane = getContentPane();
  private SimulationSettings settings;
  
  /** Creates a new instance of GUIPlate */
  public EarthSim(String[] options) {
    super("Earth Heat Simulation");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(Constants.FRAME_WIDTH, Constants.FRAME_HEIGHT));
    setLocation(100, 0);
    settings = new SimulationSettings(new EarthImage());
    setOptions(options);
  }


  private void createAndShowGUI() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    EarthPanel pnlEarth = new EarthPanel(settings);
    UserControlPanel pnlControls = new UserControlPanel(pnlEarth, settings);
    pnlControls.setAlignmentX(Component.LEFT_ALIGNMENT);

    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    contentPane.add(pnlControls);
    contentPane.add(pnlEarth);
    setVisible(true);
  }

  public void setOptions(String[] options) {
    for(int i = 0; i < options.length; i++) {
      if(options[i].equals(Constants.THREAD_PRES)) {
        settings.setPresentationThreaded(true);
      }
      else if(options[i].equals(Constants.THREAD_SIM)) {
        settings.setSimulationThreaded(true);
      }
      else if (options[i].equals(Constants.INITIATIVE_BUFF) ||
               options[i].equals(Constants.INITIATIVE_PULL) ||
               options[i].equals(Constants.INITIATIVE_PUSH) ) {
        if(settings.getInitiative() == null)
          settings.setInitiative(options[i]);
        else {
          System.out.println("Only one initiative option may be used.");
          System.exit(-1);
        }
      }
    }
    
    if(settings.getInitiative() == null) {
      System.out.println("You must specify exactly one of the following initiative options:");
      System.out.println("\t-t: Indicates that the simulation should transmit (push) data to the presentation");
      System.out.println("\t-r: Indicates that the presentation should receive (pull) data from the simulation");
      System.out.println("\t-b: indicates that a buffer component should serve as an intermediary between the presentation and the simulation");       
      System.exit(-1);
    }
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    final EarthSim simulation = new EarthSim(args);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        simulation.createAndShowGUI();
      }
    });
  }
}
